import cv2
import os

# ==== 参数 ====
video_path =   "/home/champrin/record_data/网盘/能量机关/2023-联调/视频/Video_2023_05_06_140707_7.avi"      # 视频路径
output_dir = "/home/champrin/record_data/网盘/能量机关/2023-联调/视频/Video_2023_05_06_140707_7_frame"               # 保存图像帧的目录
frame_interval = 30                         # 每隔多少帧抽取一帧

# ==== 获取视频名作为前缀 ====
video_name = os.path.splitext(os.path.basename(video_path))[0]

# ==== 创建输出目录 ====
os.makedirs(output_dir, exist_ok=True)

# ==== 打开视频 ====
cap = cv2.VideoCapture(video_path)

if not cap.isOpened():
    print("❌ 无法打开视频文件:", video_path)
    exit()

frame_count = 0
saved_count = 0

while True:
    ret, frame = cap.read()
    if not ret:
        break

    if frame_count % frame_interval == 0:
        frame_name = f"{video_name}_frame_{saved_count:05d}.jpg"
        cv2.imwrite(os.path.join(output_dir, frame_name), frame)
        saved_count += 1

    frame_count += 1

cap.release()
print(f"✅ 抽帧完成，保存了 {saved_count} 张图片到 '{output_dir}'")
